<template>
  <div class='xtx-carousel' @mouseenter="start"  @mouseleave="stop">
    <ul class="carousel-body">
      <li class="carousel-item" v-for="(item,index) in swipers" :key="item.id" :class="{fade:currentIndex===index}">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <a @click="toggle(-1)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="toggle(1)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span v-for="i in swipers.length" :key="i" :class="{active:currentIndex===i-1}"></span>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    swipers: {
      type: Array,
      required: true
    },
    authPlay: { // 来确定是否要进行轮播
      type: Boolean,
      default: true
    },
    duration: {
      type: Number,
      default: 2500
    }
  },
  setup  (props) {
    const currentIndex = ref(0)
    //  整一个定时器哈
    let timer = null
    const authplay = () => {
      timer = setInterval(() => {
        currentIndex.value++
        if (currentIndex.value >= props.swipers.length) {
          currentIndex.value = 0
        }
      }, 1 * props.duration)
    }
    // 使用侦听器监听数据
    watch(() => props.swipers, () => { // 按需监听对象中的某一项数据
      if (props.swipers.length > 1 && props.authPlay) { // 只有一张图就没必要去轮播哈
        authplay()
      }
    })
    // 当鼠标进入的时候停止
    const start = () => {
      if (timer) clearInterval(timer)
    }
    const stop = () => { // 当鼠标移除的时候在进行轮播
    // 可能有任务队列 先清除上一个定时器的
      if (timer) clearInterval(timer)
      authplay()
    }
    // 当点击左右箭头 来切换图片下面小圆圈
    const toggle = (n) => { // 形参控制是向哪个方向切换
      if (n === 1) { // 往左哈
        currentIndex.value++
        if (currentIndex.value >= props.swipers.length) {
          currentIndex.value = 0
        }
      } else {
        currentIndex.value--
        if (currentIndex.value < 0) {
          currentIndex.value = props.swipers.length - 1 // 不减1不行 数组循环是从0开始
        }
      }
    }
    return { currentIndex, start, stop, toggle }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel{
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel{
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0,0,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,0,0,.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev{
        left: 20px;
      }
      &.next{
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
